package cc.lj.internal.coupons.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketPackageRepository;
import cc.lj.internal.coupons.infrastructure.dao.ITicketPackageMapper;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketPackagePO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 电子券包　mysql仓储实现
 * @author liangxifeng
 * @date 2023-06-14
 */
@Repository("ticketPackageMysqlRespository")
public class TicketPackageMysqlRespository implements ITicketPackageRepository,BasePageHelper<TicketPackagePO> {
    @Autowired
    private ITicketPackageMapper mapper;

    @Override
    public List list(TicketPackagePO entityPO) {
        QueryWrapper<TicketPackagePO> wrapper = new QueryWrapper<>(entityPO);
        List<TicketPackagePO> entitys = mapper.selectList(wrapper);
        return entitys;
    }

    @Override
    public Integer updateById(TicketPackagePO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.UPDATE_TICKET_PACKAGE_FAILED);
        }
        return res;
    }

    @Override
    public TicketPackagePO getById(Integer id) {
        TicketPackagePO entityPO =  mapper.selectById(id);
        if(entityPO==null) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_PACKAGE_NOT_FOUND);
        }
        return entityPO;
    }

    @Override
    public PageInfo<TicketPackagePO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<TicketPackagePO> getList(TicketPackagePO ticketPackagePO) {

        QueryWrapper<TicketPackagePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,false,"id");

        return mapper.selectList(queryWrapper);
        //return this.list(ticketPackagePO);
    }
}
